#define  _CRT_SECURE_NO_WARNINGS 1

#include "Sort.h"

void TestBubble()
{
	int n = 50;
	int* arr = (int*)malloc(n * sizeof(int));
	if (arr == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	for (int i = 0; i < n; ++i)
	{
		arr[i] = rand() % 100;
	}

	PrintArray(arr, n);
	BubbleSort(arr, n);
	PrintArray(arr, n);

}

void TestSelect()
{
	int n = 50;
	int* arr = (int*)malloc(n * sizeof(int));
	if (arr == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	for (int i = 0; i < n; ++i)
	{
		arr[i] = rand() % 100;
	}

	PrintArray(arr, n);
	SelectSort(arr, n);
	PrintArray(arr, n);
}

void TestInsert()
{
	int n = 50;
	int* arr = (int*)malloc(n * sizeof(int));
	if (arr == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	for (int i = 0; i < n; ++i)
	{
		arr[i] = rand() % 100;
	}

	PrintArray(arr, n);
	InsertSort(arr, n);
	PrintArray(arr, n);
}

void TestShell()
{
	int n = 50;
	int* arr = (int*)malloc(n * sizeof(int));
	if (arr == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	for (int i = 0; i < n; ++i)
	{
		arr[i] = rand() % 100;
	}

	PrintArray(arr, n);
	ShellSort(arr, n);
	PrintArray(arr, n);
}

void TestHeap()
{
	int n = 50;
	int* arr = (int*)malloc(n * sizeof(int));
	if (arr == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	for (int i = 0; i < n; ++i)
	{
		arr[i] = rand() % 100;
	}

	PrintArray(arr, n);
	HeapSort(arr, n);
	PrintArray(arr, n);
}

void TestQuick_hoare()
{
	int n = 50;
	int* arr = (int*)malloc(n * sizeof(int));
	if (arr == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	for (int i = 0; i < n; ++i)
	{
		arr[i] = rand() % 100;
	}

	PrintArray(arr, n);
	QuickSort_hoare(arr, 0, n - 1);
	PrintArray(arr, n);
}

void TestQuick_fpsp()
{
	int n = 50;
	int* arr = (int*)malloc(n * sizeof(int));
	if (arr == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	for (int i = 0; i < n; ++i)
	{
		arr[i] = rand() % 100;
	}

	PrintArray(arr, n);
	QuickSort_fpsp(arr, 0, n - 1);
	PrintArray(arr, n);
}

void TestStack()
{
	Stack ST;
	StackInit(&ST);
	StackPush(&ST, 1);
	StackPush(&ST, 2);
	StackPush(&ST, 3);
	StackPush(&ST, 4);
	StackPush(&ST, 5);
	printf("StackSize: %d\n", StackSize(&ST));
	while (!StackEmpty(&ST))
	{
		printf("%d ", StackTop(&ST));
		StackPop(&ST);
	}

	StackDestory(&ST);
}

void TestQuickNonR()
{
	int n = 50;
	int* arr = (int*)malloc(n * sizeof(int));
	if (arr == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	for (int i = 0; i < n; ++i)
	{
		arr[i] = rand() % 100;
	}

	PrintArray(arr, n);
	QuickSortNonR(arr, 0, n - 1);
	PrintArray(arr, n);
}

void TestMerge()
{
	int n = 50;
	int* arr = (int*)malloc(n * sizeof(int));
	if (arr == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	for (int i = 0; i < n; ++i)
	{
		arr[i] = rand() % 100;
	}

	PrintArray(arr, n);
	MergeSort(arr, n);
	PrintArray(arr, n);
}

void TestMergeNonR()
{
	int n = 50;
	int* arr = (int*)malloc(n * sizeof(int));
	if (arr == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	for (int i = 0; i < n; ++i)
	{
		arr[i] = rand() % 100;
	}

	PrintArray(arr, n);
	MergeSortNonR(arr, n);
	PrintArray(arr, n);
}

void TestCount()
{
	int n = 50;
	int* arr = (int*)malloc(n * sizeof(int));
	if (arr == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	for (int i = 0; i < n; ++i)
	{
		arr[i] = rand() % 100;
	}

	PrintArray(arr, n);
	CountSort(arr, n);
	PrintArray(arr, n);
}

void TestOP()
{
	int n = 100000;
	int* arr1 = (int*)malloc(n * sizeof(int));
	int* arr2 = (int*)malloc(n * sizeof(int));
	int* arr3 = (int*)malloc(n * sizeof(int));
	int* arr4 = (int*)malloc(n * sizeof(int));
	int* arr5 = (int*)malloc(n * sizeof(int));
	int* arr6 = (int*)malloc(n * sizeof(int));
	int* arr7 = (int*)malloc(n * sizeof(int));
	int* arr8 = (int*)malloc(n * sizeof(int));
	int* arr9 = (int*)malloc(n * sizeof(int));
	int* arr10 = (int*)malloc(n * sizeof(int));
	int* arr11 = (int*)malloc(n * sizeof(int));

	for (int i = 0; i < n; ++i)
	{
		arr1[i] = rand() + i;
		arr2[i] = arr1[i];
		arr3[i] = arr1[i];
		arr4[i] = arr1[i];
		arr5[i] = arr1[i];
		arr6[i] = arr1[i];
		arr7[i] = arr1[i];
		arr8[i] = arr1[i];
		arr9[i] = arr1[i];
		arr10[i] = arr1[i];
		arr11[i] = arr1[i];
	}

	int begin = 0, end = 0;
	/*begin = clock();
	BubbleSort(arr1, n);
	end = clock();
	printf("BubbleSort: %d\n", end - begin);

	begin = clock();
	SelectSort(arr2, n);
	end = clock();
	printf("SelectSort: %d\n", end - begin);*/

	begin = clock();
	InsertSort(arr3, n);
	end = clock();
	printf("InsertSort: %d\n", end - begin);

	begin = clock();
	ShellSort(arr4, n);
	end = clock();
	printf("ShellSort: %d\n", end - begin);

	begin = clock();
	HeapSort(arr5, n);
	end = clock();
	printf("HeapSort: %d\n", end - begin);

	begin = clock();
	QuickSort_hoare(arr6, 0, n - 1);
	end = clock();
	printf("QuickSort_hoare: %d\n", end - begin);

	begin = clock();
	QuickSort_fpsp(arr7, 0, n - 1);
	end = clock();
	printf("QuickSort_fpsp: %d\n", end - begin);

	begin = clock();
	QuickSortNonR(arr8, 0, n - 1);
	end = clock();
	printf("QuickSortNonR: %d\n", end - begin);

	begin = clock();
	MergeSort(arr9, n);
	end = clock();
	printf("MergeSort: %d\n", end - begin);

	begin = clock();
	MergeSortNonR(arr10, n);
	end = clock();
	printf("MergeSortNonR: %d\n", end - begin);

	begin = clock();
	CountSort(arr11, n);
	end = clock();
	printf("CountSort: %d\n", end - begin);

}

int main()
{
	srand((unsigned int)time(NULL));
	//TestBubble();
	//TestSelect();
	//TestInsert();
	//TestShell();
	//TestHeap();
	//TestQuick_hoare();
	//TestQuick_fpsp();
	//TestStack();
	//TestQuickNonR();
	//TestMerge();
	//TestMergeNonR();
	//TestCount();
	TestOP();


	return 0;
}